package com.lehecai.ucenter.interceptor;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lehecai.ucenter.action.BaseAction;
import com.lehecai.ucenter.bean.UserSessionBean;
import com.lehecai.ucenter.constant.GlobalConstant;
import com.lehecai.ucenter.entity.permission.Permission;
import com.lehecai.ucenter.service.permission.PermissionService;
import com.lehecai.ucenter.utils.StringUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class UserSessionInterceptor extends AbstractInterceptor {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 2026179405489779948L;

	private final Logger logger = LoggerFactory.getLogger(UserSessionInterceptor.class);

	private PermissionService permissionService;
	
	public String intercept(ActionInvocation invocation) throws Exception{
		boolean pass = false;
		logger.info("Enter UserSessionInterceptor");
		ActionContext ac = invocation.getInvocationContext();
		String actionName = ac.getName();
		Map<?, ?> paramMap = ac.getParameters();
		logger.info("ActionName:{}",actionName);
		logger.info("Parameters:{}",paramMap.toString());
		UserSessionBean userSessionBean = (UserSessionBean) ac.getSession().get(GlobalConstant.USER_SESSION_KEY);
		
		if(userSessionBean == null) {
			BaseAction basetion = (BaseAction) invocation.getAction();
			basetion.setErrorMessage("您的session丢失，请重新登录");
			return "index";
		}
		String message = StringUtil.getStringByActionHashMap(paramMap);
		if(message != null && message.length() >= 1000){
			message = message.substring(0,900);
		}
		logger.info("Start judge permission");
		List<Permission> permissions = userSessionBean.getPermissions();
		for(Permission permission : permissions){
			if(permission.getActionName().trim().equals(actionName.trim())){
				if(!StringUtils.isEmpty(permission.getParamName())){
					String[] paramValue = (String[]) paramMap.get(permission.getParamName());
					if(paramValue == null || paramValue.length == 0) {
						continue;
					}
					if(!paramValue[0].equals(permission.getParamValue())){
						continue;
					}
				}
				String[] actions = (String[]) paramMap.get("action");
				if (actions == null || actions.length == 0) {
					pass = true;
					break;
				}
				String action = actions[0];
				if ("handle".equals(action)){
					pass = true;
					break;
				}
				
				if(permission.getMethodName()!= null && permission.getMethodName().equals(action)){
					pass = true;
					break;
				}
			}
		}
		logger.info("End judge permission");
		logger.info("Judge permission result:{}", pass);
		logger.info("Exit UserSessionInterceptor");
		if(pass){
			return invocation.invoke();
		}else{
			BaseAction basetion = (BaseAction) invocation.getAction();
			basetion.setErrorMessage("没有权限操作");
			return "failure";
		}
	}

	public PermissionService getPermissionService() {
		return permissionService;
	}

	public void setPermissionService(PermissionService permissionService) {
		this.permissionService = permissionService;
	}
}
